<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="sendcar">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      录入成本价格维护单
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="../../assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="" method="post" id="formid">
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">所属公司</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select orgguid" name="entity.orgid" v-model="orgguid">
                <option selected="" v-for="(item, index) in orgs" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">库房</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select srguid" name="entity.srguid" v-model="srguid">
                <option selected=""  v-for="(item, index) in storerooms" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select productid" name="entity.productid" v-model="productid">
                <option selected=""  v-for="(item, index) in products" :key="item.productid" :value="item.productid">{{item.productname}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="specs.length > 0">
            <div class="weui-cell__hd"><label class="weui-label">规格</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="entity.speguid" v-model="speguid">
                <option selected=""  v-for="(item, index) in specs" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">单价(元/吨)</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input price" type="number" name="entity.price" :placeholder="pricePlaceholder" />
            </div>
          </div>
        </div>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn"  href="javascript:" @click="send">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'AddChangeCostPrice',
    data () {
      return {
        openid: localStorage.openid,
        orgs: [],
        storerooms: [],
        products: [],
        specs: [],
        orgguid: '',
        srguid: '',
        productid: '',
        speguid: '',
        avgprice: 0
      }
    },
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      //获取select备选数据
      this.getBaseInfo();
      this.getSpec();
    },
    computed: {
      pricePlaceholder: function () {
        if (this.avgprice != 0) {
          return '参考值：' + this.avgprice;
        } else {
          return '请输入调整重量';
        }
      }
    },
    watch: {
      'srguid': function (val) {
        this.getProduct();
        this.getSpec();
      },
      'productid': function (val) {
        this.getSpec();
      },
    },
    methods: {
      // pricePlaceholder: function() {
      //
      // },
      getBaseInfo: function() {
        this.axios({
          method: 'get',
          url: '/produce/Costprice!getBaseInfo.action?openid=' + this.openid,
          params: {}
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.orgs = response.data.orgs;
            this.orgguid = response.data.orgs[0].guid;
            this.storerooms = response.data.rooms;
            this.srguid = response.data.rooms[0].guid;
            this.specs = response.data.spes;
            this.speguid = response.data.spes[0].guid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getProduct: function() {
        this.axios({
          method: 'get',
          url: '/produce/Costprice!getProducts.action',
          params: {srguid: this.srguid}
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.products = response.data.infos;
            this.productid = response.data.infos[0].productid;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      getSpec: function() {
        if (this.srguid == '' || this.productid == '') return;
        this.axios({
          method: 'get',
          url: '/produce/Costprice!getAvgPrice.action?openid=' + this.openid,
          params: {srguid: this.srguid, productid: this.productid}
        }).then((response) => {
            if (response.data.msg == 'success') {
            this.avgprice = response.data.avgprice;
          } else {
            this.toastError('系统错误');
          }

        })
      },
      send: function() {
        if (!this.checkEmy('orgguid')) {
          this.toastText('公司不能为空！');
          return false;
        }
        if (!this.checkEmy('srguid')) {
          this.toastText('库房不能为空！');
          return false;
        }
        if (!this.checkEmy('productid')) {
          this.toastText('产品不能为空！');
          return false;
        }
        if (!this.checkEmy('price')) {
          this.toastText('单价不能为空！');
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");

        this.axios({
          method: 'post',
          url: '/produce/Costprice!save.action?openid=' + this.openid,
          params: this.serialize2Obj($("#formid").serializeArray())
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");

          if(response.data.msg == "success"){
            this.toastSuccess('操作成功!');
            this.$router.push({name: 'ChangeCostPriceList'});
          }else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          this.toastError('系统错误');
        });

      },
    }
  }


</script>
